# -*- coding: utf-8 -*-
import os
from time import sleep
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from pysql import  *;

global page;
page=720;
global currentnum;
currentnum=0;
#当下载到第九次的时候，需要重新登录一次
global totalcount;
totalcount=0;


def init(dic,loadurl):
    options = webdriver.ChromeOptions()
    prefs = {'profile.default_content_settings.popups': 0, 'download.default_directory': 'd:\\'}
    options.add_experimental_option('prefs', prefs)
    driver=webdriver.Chrome(chrome_options=options);
    return driver;

def userlogin(driver,username,password):
    WebDriverWait(driver, 5).until(EC.visibility_of(driver.find_element(by=By.ID, value='showlogin')))
    #username="18610359049";
    #password="059230";
    logframe=driver.find_element_by_id('showlogin');
    logframe.click();
    driver.switch_to.frame('dialogLoginIframe');
    input_name=driver.find_element_by_name('username');
    input_name.send_keys(username);
    input_password=driver.find_element_by_name('password');
    input_password.send_keys(password);
    input_submit=driver.find_element_by_id('CommonLogin');
    input_submit.click();

#退出登录
def quitLogin(driver):
    quit=driver.find_element_by_xpath("//div[@class='land']/a[2]");
    action = ActionChains(driver);
    action.move_to_element(quit).perform()
    quit.click();



def subclassPage(driver):
    while 1:
        subs=driver.find_elements_by_class_name('softTitle')
        for i in range(len(subs)):
            if i>=currentnum:
                dealsubClass(driver,i)
        #点击下一页
        next=driver.find_element_by_xpath("//div[@class='page cf']/a[last()-1]");
        hr=next.get_attribute('href');
        start=hr.index('pn')+3;
        end=hr.index("')")
        pn=hr[start:end];
        global page;
        page=int(pn);
        action = ActionChains(driver);
        action.move_to_element(next).perform()
        sleep(2);
        next.click();
        #点击后等待五秒钟
        print('click nextpage=%d' %(page));
        WebDriverWait(driver, 2).until(EC.visibility_of(driver.find_element(by=By.CLASS_NAME, value='softTitle')))
        global  currentnum;
        currentnum=0;
def log():
    global page;
    global currentnum;
    global totalcount;
    print('current download page=%d,currentnum=%d,totalcount=%d' %(page,currentnum,totalcount));


def download(driver):
    #打开立即下载frame
    WebDriverWait(driver, 5).until(EC.visibility_of(driver.find_element(by=By.CLASS_NAME, value='download')))
    driver.find_elements_by_class_name('download').click();



def toScrapy(username,password):
    try:
        numPerAccount=10;
        sp=ScrapyPaper();
        datalist=sp.getDownUrl();
        for p in datalist:
            downurl=''
            xkwid='';
        driver = init(xkwid,downurl);
        print('scrpay url=%s'%(downurl));
        driver.get(downurl)
        print('begin login !!!!');
        userlogin(driver,username,password);
        print('login success!!!')
    except Exception as e:
        raise e;
        driver.quit();
        toScrapy();

account=[('a1','p1'),('a2','p2')]
for a in account:
    toScrapy(a[0],a[1])



